// filterSearch  by  adam  2016-1-6
// 筛选搜索
;(function($){
	$.extend($.fn,{
		filterSearch : function(options){
			var options = $.extend(true,{},$.fn.filterSearch.defaults,options);
			var _this=this;
			var $this=$(this);
			var this_id=$this.attr("id");
			var contentHtml = '<span id="'+options.filterId+'">'+options.filterName+'：</span>';
			contentHtml +='<a value="" class="fs_selected">全部</a>';
			var filterValue = options.filterValue;
			var orderList = options.orderList;
			if(orderList.length > 0){
				for(var i in orderList){
					var fkey =orderList[i];
					var filterOptionHtml = '<a value="'+filterValue[fkey]+'">'+fkey+'</a>';
					contentHtml +=filterOptionHtml;	
				}
			}else{
				for(var i in filterValue){
					var filterOptionHtml = '<a value="'+filterValue[i]+'">'+i+'</a>';
					contentHtml +=filterOptionHtml;	
				}
			}
			contentHtml ='<div class="fs_filterDiv">'+contentHtml+'</div>';
			$(this).append(contentHtml);	

			//筛选结果div
			var filterResult =this_id+"_filterResult";
			if(!document.getElementById(filterResult)){//如果已经存在就不再创建
				var filterResultDiv =''+
					'<div class="fs_filterDiv fs_select-result" id="'+filterResult+'">'+
					'	<span>筛选条件：</span>'+
					'	<span class="fs_select-no">暂时没有选择过滤条件</span>'+
					'</div>'
				$(this).after(filterResultDiv);
			}
			
			var selectA="fs_select_"+options.filterId; //存放筛选条件的div
			var $selectA =$("#"+selectA);
			//给筛选条件里面的div添加点击删除方法
			$(document).on("click","#"+selectA, function () {
				$(this).remove();
				$("#"+$(this).attr("rel")).siblings("a:first").trigger("click");
			});
			
			//绑定点击事件
			$("#"+options.filterId).siblings("a").on("click",function(){
				$(this).addClass("fs_selected").siblings().removeClass("fs_selected");
				var filterId =$(this).prevAll("span:first-child").attr("id");//筛选的字段
				var filterVal=$(this).attr("value");//筛选字段的值
				$this.data(filterId,filterVal);
				if(filterVal){ //筛选字段的值存在  存储筛选字段和值
					var copyThisA = $(this).clone();//克隆当前选项
					if ($("#"+selectA).length > 0) {
						$("#"+selectA).html($(this).text());
					} else {
						$("#"+filterResult).append(copyThisA.attr({"id":selectA,"rel":options.filterId}));
					}
				}else{//否则 删除筛选字段和值
					//$this.removeData(filterId);
					$("#"+selectA).remove();//移除筛选项
				}
				
				if ($("#"+filterResult+">a").length > 0) {
					$this.parent().find(".fs_select-no").hide();
				} else {
					$this.parent().find(".fs_select-no").show();
				}
				
			});

		},
		//加强版的搜索
		strongFilterSearch : function(options){
			var $this=$(this);
			var options = $.extend(true,{},$.fn.filterSearch.defaults,options);
			$(this).addClass("b_page-main-contant sousuo col-xs-12");
			var this_id =$(this).attr("id");
			var filterDiv=this_id+"_filterSearch";
			var input_id =this_id+"_input";//搜索输入框id
			var input_btn_id=this_id+"_search";//搜索按钮id
			if(!document.getElementById(filterDiv)){//如果已经存在就不再创建
				var moreFilter =filterDiv+"_more";
				var btnMoreFilter =filterDiv+"_btn_more";
				var moreBtnShow =options.moreBtnShow;
				var moreBtnHtml="";
				if(!moreBtnShow){
					moreBtnHtml="display:none;";
				}
				
				var filterDivHide = options.filter==null || options.filter.length==0;
				if(filterDivHide){
					$("#"+filterDiv).hide();
					$("#"+this_id+'_filterResult').hide();
				}
				var dateDivHide = options.datepicker==null || options.datepicker.length==0
				if(dateDivHide){
					$(".dateDiv").hide();
				}
				var franchiseeDivHide = options.searchFranchisee==null || options.searchFranchisee.length==0
				if(franchiseeDivHide){
					$(".franchiseeDiv").hide();
				}
				var cityDivHide = options.searchCity==null || options.searchCity.length==0
				if(cityDivHide){
					$(".cityDiv").hide();
				}
				if(filterDivHide && dateDivHide && franchiseeDivHide&& cityDivHide){
					moreBtnHtml="display:none;";
				}
				
				var this_html=''+
					'<div class="col-lg-4 col-xs-5 lui-pdf0">'+
					'	<input type="text" class="col-lg-12 col-xs-12 pull-right" id="'+input_id+'" placeholder="'+options.inputPlaceholder+'" value="'+options.inputValue+'" >'+
					'</div>'+
					'<div>'+
					'	<a type="button" class="btn btn-primary  no-border jg-ss-but" id="'+input_btn_id+'">'+
					'		<i class="ace-icon fa fa-search icon-on-right bigger-110"></i>'+
					'	</a>'+
					'</div>'+
					'<div style="margin-left:10px;'+moreBtnHtml+'" id="'+btnMoreFilter+'" >'+
					'	<a type="button" class="btn btn-white btn-default no-border jg-ss-but2" href="#'+moreFilter+'" data-toggle="collapse" > 更多'+ 
					'		<i class="ace-icon fa fa-chevron-down  smaller-80" data-icon-hide="ace-icon fa fa-chevron-up align-top" data-icon-show="ace-icon fa fa-chevron-down" id="imgcoll"></i>'+
					'	</a>'+
					'</div>'+
					'<div class="panel-collapse collapse col-md-12 lui-pdf2 " id="'+moreFilter+'" >'+
					'	<div id="'+filterDiv+'"></div>'+
					'</div>';
				$(this).html(this_html);
				
				//搜索按钮点击事件
				$("#"+input_btn_id).on("click",function(){
					$this.data(options.inputFiled,$("#"+input_id).val() );
					var filterData=$("#"+filterDiv).data();//快捷搜索列表中的值
					var searchData=$this.data();//搜索框的值
					var data=jQuery.extend(filterData,searchData);
					//时间
					$(".dateDiv input").each(function(){
						if($(this).val()){
							data[$(this).attr("id")] = $(this).val();
						}else{
							data[$(this).attr("id")] = null;
						}
					});
					//加盟商
					$(".franchiseeDiv input").each(function(){
						if($(this).val()){
							data[$(this).attr("id")] = $(this).val();
						}else{
							data[$(this).attr("id")] = null;
						}
					});
					//地区
					$(".cityDiv input").each(function(){
						if($(this).val()){
							data[$(this).attr("id")] = $(this).val();
						}else{
							data[$(this).attr("id")] = null;
						}
					});
					if(options.searchCallback){
						options.searchCallback(data);
					}
				});
				
				//输入框回车键响应事件
				$("#"+input_id).on("keypress",function(e){
					var e = e || window.event;
					if (e.keyCode == 13) {
						$("#"+input_btn_id).trigger("click");// 方法
					}
				});
				
				//更多按钮点击回调
				$("#"+btnMoreFilter).click(function(){
					if(options.moreBtnCallBack){
						options.moreBtnCallBack($("#"+moreFilter))
					}
					
				})
				
			}
			
			for(var i in options.filter){
				$("#"+filterDiv).filterSearch(options.filter[i]);
			}
			
			//添加时间框
			if(options.datepicker){
				$this.createDatepicker(options)
			}
			//添加加盟商下拉树
			if(options.searchFranchisee){
				$this.createFranchisee(options)
			}
			//添加地区下拉框
			if(options.searchCity){
				$this.createCity(options)
			}
			//添加按钮上拉框；
			$this.btnSelect();
			return this;
		},
		createDatepicker : function(options){
			var $this=$(this);
			var options = $.extend(true,{},$.fn.filterSearch.defaults,options);
			var this_id =$(this).attr("id");
			var moreFilter=this_id+"_filterSearch_more";
			
			//添加 时间搜索框
			var datepickerArr = options.datepicker;
			for(var i in datepickerArr){
				var datepicker =datepickerArr[i];
				var id =datepicker.id;
				var name =datepicker.name;
				var type =datepicker.type;
				var opt = datepicker.opt;
				//时间范围
				if($.isArray(id)){
					var dateDiv =
						'		<div class="col-sm-3 col-xs-12 dateDiv">'+
						'			<div class="form-group">'+
						' 				<div class="col-xs-4 dateNameDiv" style="height:34px;padding: 0px;	line-height: 34px;">'+
										name +
						'				：</div>'+
						'				<div class="input-group col-xs-8">'+
						'					<input type="text" id="'+id[0]+'" class="form-control" readonly/>'+
						'					<span class="input-group-addon"><i class="fa fa-calendar bigger-110"></i> </span>'+
						'				</div>' +
						'			</div>' +
						'		</div>' +
						'		<div class="col-sm-3 col-xs-12 dateDiv">'+
						'			<div class="form-group">'+
						' 				<div class="col-sm-2 col-xs-4" style="height:34px;padding: 0px;	line-height: 34px;">'+
						'				—	'+
						'				</div>'+
						'				<div class="input-group col-sm-8 col-xs-8">'+
						'					<input type="text" id="'+id[1]+'" class="form-control" readonly/>'+
						'					<span class="input-group-addon"><i class="fa fa-calendar bigger-110"></i> </span>'+
						'				</div>' +
						'			</div>' +
						'		</div>' ;
					$("#"+moreFilter).append(dateDiv);
					switch(type){
						case 'datepicker':
							//时间范围
							$("#"+id[0]).datetimepicker({
								format:'Y-m-d',		//格式化
								timepicker:false,	//不允许显示时间
								onShow:function(){
									var end=$("#"+id[1]).val();
									if(end){
										end =end.split(" ");
										this.setOptions({
											maxDate:end[0]
										});
									}
								}
							}).next().on(ace.click_event, function(){
								$(this).prev().focus();
							});
							$("#"+id[1]).datetimepicker({
								format:'Y-m-d',		//格式化
								timepicker:false,	//不允许显示时间
								onShow:function( ct ){
									var start=$("#"+id[0]).val();
									if(start){
										start =start.split(" ");
										this.setOptions({
											minDate:start[0]
										});
									}
								}
							}).next().on(ace.click_event, function(){
								$(this).prev().focus();
							});
							break;
						case 'datetimepicker':
							//时间范围
							$("#"+id[0]).datetimepicker(
								lui.setRangeStartTime("#"+id[1])
							).next().on(ace.click_event, function(){
								$(this).prev().focus();
							});
							$("#"+id[1]).datetimepicker(
								lui.setRangeEndTime("#"+id[0])
							).next().on(ace.click_event, function(){
								$(this).prev().focus();
							});
							break;
					}
				}else{
					var dateDiv =
						'		<div class="col-sm-3 col-xs-12 dateDiv">'+
						'			<div class="form-group">'+
						' 				<div class="col-xs-4 dateNameDiv" style="height:34px;padding: 0px;	line-height: 34px;">'+
										name +
						'				：</div>'+
						'				<div class="input-group col-xs-8">'+
						'					<input type="text" id="'+id+'" class="form-control" readonly/>'+
						'					<span class="input-group-addon"><i class="fa fa-calendar bigger-110"></i> </span>'+
						'				</div>' +
						'			</div>' +
						'		</div>';
					$("#"+moreFilter).append(dateDiv);
					switch(type){
						case 'datepicker':
							opt = $.extend(true,opt,{format:'Y-m-d',timepicker:false});
							$("#"+id).datetimepicker(opt)
							.next().on(ace.click_event, function(){
								$(this).prev().focus();
							});
							break;
						case 'datetimepicker':
							opt = $.extend(true,opt,{step:15});
							$("#"+id).datetimepicker(opt)
							.next().on(ace.click_event, function(){
								$(this).prev().focus();
							});
							break;
					}
				}
			}
		},
		
		//---------------createFranchisee   start------------------
		createFranchisee : function(options){
			var $this=$(this);
			var options = $.extend(true,{},$.fn.filterSearch.defaults,options);
			var this_id =$(this).attr("id");
			var moreFilter=this_id+"_filterSearch_more";
			
			//添加 加盟商下拉树
			var franchiseeArr = options.searchFranchisee;
			for(var i in franchiseeArr){
				var franchisee =franchiseeArr[i];
				var id =franchisee.id;
				var name =franchisee.name;
				//var type =datepicker.type;
				var opt = franchisee.opt;
				//时间范围
				if($.isArray(id)){
					var franchiseeDiv =
						'		<div class="col-sm-3 col-xs-12 franchiseeDiv">'+
						'			<div class="form-group">'+
						' 				<div class="col-xs-4 franchiseeNameDiv" style="height:34px;padding: 0px;	line-height: 34px;">'+
										name +
						'				：</div>'+
						'				<div class="input-group col-xs-8">'+'<input type="text" id="deptId"  style="display:none" />'+
						'					<input type="text" id="'+id[0]+'" class="col-xs-12 col-sm-12"  />'+
						'				</div>' +
						'			</div>' +
						'		</div>';
					$("#"+moreFilter).append(franchiseeDiv);
				}else{
					var franchiseeDiv =
						'		<div class="col-sm-3 col-xs-12 franchiseeDiv">'+
						'			<div class="form-group">'+
						' 				<div class="col-xs-4 franchiseeNameDiv" style="height:34px;padding: 0px;	line-height: 34px;">'+
										name +
						'				：</div>'+
						'				<div class="input-group col-xs-8">'+
						'					<input type="text" id="'+id+'" class="form-control" readonly/>'+
						'				</div>' +
						'			</div>' +
						'		</div>';
					$("#"+moreFilter).append(franchiseeDiv);
				}
			}
		},
		//---------------createFranchisee  end--------------------
		//---------------createCity   start------------------
		createCity : function(options){
			var $this=$(this);
			var options = $.extend(true,{},$.fn.filterSearch.defaults,options);
			var this_id =$(this).attr("id");
			var moreFilter=this_id+"_filterSearch_more";
			
			//添加地区下拉框
			var cityArr = options.searchCity;
			for(var i in cityArr){
				var city =cityArr[i];
				var id =city.id;
				var name =city.name;
				//var type =datepicker.type;
				var opt = city.opt;
				//时间范围
				if($.isArray(id)){
					var cityDiv =
						'		<div class="col-sm-3 col-xs-12 cityDiv">'+
						'			<div class="form-group">'+
						' 				<div class="col-xs-4 cityNameDiv" style="height:34px;padding: 0px;	line-height: 34px;">'+
										name +
						'				：</div>'+
						'				<div class="input-group col-xs-8">'+'<input type="text" id="areaId"  style="display:none" />'+
						'					<input type="text" id="'+id[0]+'" class="col-xs-12 col-sm-9"    />'+
						'				</div>' +
						'			</div>' +
						'		</div>';
					$("#"+moreFilter).append(cityDiv);
				}else{
					var cityDiv =
						'		<div class="col-sm-3 col-xs-12 cityDiv">'+
						'			<div class="form-group">'+
						' 				<div class="col-xs-4 cityNameDiv" style="height:34px;padding: 0px;	line-height: 34px;">'+
										name +
						'				：</div>'+
						'				<div class="input-group col-xs-8">'+
						'					<input type="text" id="'+id+'" class="form-control" readonly/>'+
						'				</div>' +
						'			</div>' +
						'		</div>';
					$("#"+moreFilter).append(cityDiv);
				}
			}
		}
		//---------------createCity  end--------------------
	});
	//默认参数
	$.fn.filterSearch.defaults={
		filter:null,
		filterName : null,
		filterId : null,
		filterValue : null,
		orderList : [] ,
		inputPlaceholder:"请输入搜索值",//占位符
		inputFiled:"searchValue", //搜索字段
		inputValue:"", //搜索值
		searchCallback : null,//搜搜按钮点击后回调
		moreBtnShow:true, //更多按钮显示
		moreBtnCallBack :null, //点击更多按钮时的回调
		datepicker : null,
		searchFranchisee:null,
		searchCity:null
	};
})(jQuery);

;(function($){
	$.extend($.fn,{
		btnSelect : function(options){
			var options = $.extend(true,{},$.fn.btnSelect.defaults,options);
			var btn_s_id =this[0].id+"_btnSelect";
			var btn_s='<div class="btn-group pager_btn_select " id="'+btn_s_id+'">' 
					+		'<button type="button" class="btn dropdown-toggle"  data-toggle="dropdown" style="display: none;"></button>'
					+		'<ul class="dropdown-menu" role="menu" style="min-width:100px;">'
					+		'</ul>'
					+	'</div>';
			$(this).parent().append(btn_s);
			return this;
		},
		addBtnSelect :function(options){
			var options = $.extend(true,{},$.fn.btnSelect.defaults,options);
			var li="";
			for(var i in options.data){
				var d =options.data[i];
				if(d["click"]){
					li +='<li><a onclick =\''+d["click"]+'('+JSON.stringify(d["clickOpt"]).toString()+')\' >'+d["name"]+'</a></li>';
				}else{
					li +='<li><a href="'+d["url"]+'">'+d["name"]+'</a></li>';
				}
			}
			$(".pager_btn_select ul").html(li).css({width:options.width});
			var btn_offset =$(this).offset();
			var btn_top =btn_offset.top;
			var btn_left=btn_offset.left;
			
			var $btn_select = $(".pager_btn_select");
			$btn_select.css({"top":"0px","left":"0px"});
			
			var btn_select_h =$btn_select.find("ul:first").outerHeight();
			var btn_select_w =$btn_select.find("ul:first").outerWidth();
			var btn_select_offset =$btn_select.offset();
			var btn_select_top=btn_select_offset.top;
			var btn_select_left=btn_select_offset.left;
			
			var row_offset =$(this).parents(".row:first").offset();
			var top=-(btn_select_top-btn_top+btn_select_h)+"px";
			left =(btn_left-row_offset.left-10)+"px";
			
			if(options.data && options.data.length>0){
				$btn_select.css({"top":top,"left":left})
					.find("button:first").click();
			}
		}
	});
	//默认参数
	$.fn.btnSelect.defaults={
		width:null,
		data:[]
	};
})(jQuery);



;(function($){
	 $.extend($.fn,{
		gridAddRowData :function(options){
			var newRowData = $.extend(true,{},$.fn.gridAddRowData.defaults,options);
	        var $table = $(this);  
	        var ids = $table.jqGrid('getDataIDs');
	        //如果jqgrid中没有数据 定义行号为1 ，否则取当前最大行号+1    
	        var newRowId = "_" + (ids.length ==0 ? 1: ids.length+1);
	        if(!newRowData["id"]){
	        	newRowData["id"] =newRowId;
	        }
	        $table.jqGrid("addRowData", newRowId,newRowData,"last");    
		},
		girdGetChangeData :function(options){
			var options = $.extend(true,{},$.fn.girdGetChangeData.defaults,options);
			var $table = $(this);  
			var initData = $table.data("initData");
	    	var newData = $table.getAllData();
	    	var copyInitData =$.extend(true,{}, initData);//创建一个 数据的拷贝
	    	var copyNewData =$.extend(true,{}, newData);//创建一个 数据的拷贝
	    	var changeDataArr =[];
			for(var i = 0 , iLen = initData.length ; i<iLen ; i++ ){
				for(var j = 0 , jLen =newData.length ; j<jLen ; j++){
					var initObj = initData[i];
					var newObj = newData[j];
					var changeData = newObj;
					if(initObj["id"] === newObj["id"]){
						delete copyInitData[i];
						delete copyNewData[j];
						for(var k in initObj){
							if(initObj[k] !== newObj[k]){ 
								changeData[options.rowChangeState]= 1;//如果数据有不相等的，则为编辑状态
								if(changeData["myac"]){
									delete changeData["myac"];
								}
								changeDataArr.push(changeData);
								break;
							}
						}
					}
				}
			}
			for(var i in copyInitData){
				var copyInitDataI =copyInitData[i];
				copyInitDataI[options.rowChangeState] = 2 ;//数据为删除状态；
				if(copyInitDataI["myac"]){
					delete copyInitDataI["myac"];
				}
				changeDataArr.push(copyInitDataI);
			}
			for(var i in copyNewData){
				var copyNewDataI = copyNewData[i];
				copyNewDataI[options.rowChangeState] = 0 ;//数据为新增状态；
				if(copyNewDataI["myac"]){
					delete copyNewDataI["myac"];
				}
				delete copyNewDataI["id"];//删除id属性
				changeDataArr.push(copyNewDataI);
			}
			return changeDataArr;
		}
	});
		//默认参数
		$.fn.gridAddRowData.defaults={
		};
		//默认参数
		$.fn.girdGetChangeData.defaults={
			rowChangeState :"rowChangeState" //（0 新增状态  ；1 编辑状态 ； 2 删除状态）
		};
})(jQuery);

